﻿///////////////////////////////////////////////////////////////////////////////////////////////
// System.Text.Parsing - © 2011 Sebastien Pellizzari
// Released under the MIT License (http://www.opensource.org/licenses/mit-license.php)
///////////////////////////////////////////////////////////////////////////////////////////////

namespace System.Text.Parsing
{
    /// <summary>Represents a text in a grammar file.</summary>
    public struct GrammarTextValue
    {
        private readonly string _text;                      // The text value.
        private readonly GrammarSourceRange _sourceInfo;    // The text source.

        //-----------------------------------------------------------------------
        //     Properties
        //-----------------------------------------------------------------------

        /// <summary>Gets the text value.</summary>
        public string Text { get { return _text; } }
        /// <summary>Gets the text source information.</summary>
        public GrammarSourceRange SourceInfo { get { return _sourceInfo; } }

        /// <summary>Represents an empty value.</summary>
        public static readonly GrammarTextValue Empty = new GrammarTextValue(String.Empty, GrammarSourceRange.Unknown);

        //-----------------------------------------------------------------------
        //     Constructors
        //-----------------------------------------------------------------------

        /// <summary>Initializes an instance of the <see cref="GrammarTextValue"/> class.</summary>
        /// <param name="text">The text value.</param>
        /// <param name="sourceInfo">The source information.</param>
        public GrammarTextValue(string text, GrammarSourceRange sourceInfo)
        {
            if (text == null) throw new ArgumentNullException("text");
            _text = text;
            _sourceInfo = sourceInfo;
        }

        //-----------------------------------------------------------------------
        //     Operators
        //-----------------------------------------------------------------------

        /// <summary>Implicitly converts a string value into a <see cref="GrammarTextValue"/> with unknown source info.</summary>
        public static implicit operator GrammarTextValue(string value)
        {
            return new GrammarTextValue(value, GrammarSourceRange.Unknown);
        }
    }
}
